package com.mzmarket.market.app.controller;

import com.mzmarket.market.app.domain.CategoryCellVO;
import com.mzmarket.market.app.domain.CategoryListVO;
import com.mzmarket.market.module.entity.Category;
import com.mzmarket.market.module.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/category/list")
    public CategoryListVO categoryAll() {
        List<Category> categoryList = null;
        try {
            categoryList = categoryService.getCategoryList();
        } catch (Exception e) {
            log.warn(e.getMessage());
        }

        if (categoryList != null) {
            List<CategoryCellVO> categoryCellVOList = new ArrayList<>();
            for (Category category : categoryList) {
                categoryCellVOList.add(new CategoryCellVO().setName(category.getName())
                        .setId(category.getId())
                        .setImage(category.getImage()));
            }
            return new CategoryListVO().setList(categoryCellVOList);
        }else{
            return  new CategoryListVO();
        }
    }
}
